package com.zufangbao.earth.web.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.DateUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Secure;
import com.zufangbao.earth.email.GenerateEmailOrExcelDataHandler;
import com.zufangbao.earth.entity.security.Principal;
import com.zufangbao.sun.entity.company.corp.App;
import com.zufangbao.sun.service.AppService;

@Controller
@RequestMapping("")
@MenuSetting("menu-system")
public class EmailSenderController {
	
	@Resource
	private GenerateEmailOrExcelDataHandler emailSender;
	@Resource
	private AppService appService;
	
	
	@RequestMapping(value="send-email")
	public @ResponseBody String sendEmailWithExcel(@Secure Principal principal ,@RequestParam(value = "app",required=true) String app){
		Long appId = Long.valueOf(app);
		Date report_date = new Date();
		String today = DateUtils.format(report_date, "yyyy-MM-dd 00:00:00");
		report_date = DateUtils.parseDate(today, "yyyy-MM-dd 00:00:00");
		App real_app = this.appService.load(App.class, appId);
		String companyName = real_app.getName();
		String excelName = " 鼎程&"+companyName+"融资回款对账信息";
		try {
			this.emailSender.sendDailyReportForPartners(appId,report_date,excelName,companyName);
		} catch (Exception e) {
			return "邮件发送异常";
		}
		return "邮件已发送";
	}
	
	@MenuSetting("submenu-mail-manager")
	@RequestMapping("email-manager")
	public ModelAndView sendEmailPage(){
		ModelAndView result = new ModelAndView("email/email-manager");
		List<App> appList = this.appService.loadAll(App.class);
		result.addObject("appList", appList);
		return result;
	}
}
